<<<<<<< HEAD
const express = require('express')
const app = express()
const port = 3000

const db = require('./SQLHelper')

const Snowflake = require('./Snowflake')

const router = require('./router/index')

var bodyParser = require('body-parser')

require('body-parser-xml')(bodyParser)

app.use(bodyParser.json({ limit:'100mb' }))
app.use(bodyParser.urlencoded({ limit:'100mb', extended: true }))

app.all('*', function (req, res, next) {
  res.header("Access-Control-Allow-Origin", '*')
  res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')
  res.header('Access-Control-Allow-Headers', 'X-Requested-With,Content-Type,token');
  next()
})

router(app,loginRouter,db)

app.get('/getTest', (req, res) => {
  res.send({ msg:'Get Hello World!' })
})

let tokens = { },tokenTimes = {}

app.post('/login',async (req, res) => {
  let userName = req.body.userName,passWord = req.body.passWord
  if(!userName || !passWord) {
    res.send({ errorMsg:'请输入用户名和密码' })
    return
  }
  let user = await db.query(`select * from user where userName="${userName}";`)
  if(!user[0]) {
    res.send({ errorMsg:'用户名不存在' })
    return
  }
  user = user[0]
  if(user.passWord == passWord){
    let token = Snowflake()
    tokens[token] = user
    tokenTimes[token] = new Date()
    res.send({ token, msg:'登录成功' })
  }else{
    res.send({ code:400,errorMsg:'密码错误' })
  }
})

function loginRouter(req,res,next){
  let token = req.headers.token
  if(!token){
    res.send({ code:400,errorMsg:'请登录后调用接口' })
  }else if(!tokens[token]){
    res.send({ code:400,errorMsg:'无效的token' })
  }else if(new Date().getTime() - tokenTimes[token].getTime() > 2 * 60 * 60 * 1000){
    res.send({ code:402,errorMsg:'登录已过期,请重新登录' })
  }else{
    req.session = tokens[token]
    next()
  }
}

app.get('/userInfo',loginRouter, (req, res) => {
  res.send(req.session)
})

app.get('/logout',loginRouter, (req, res) => {
  let token = req.headers.token
  tokens[token] = undefined
  tokenTimes[token] = undefined
  res.send({ msg:'退出登录' })
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
=======
const express = require('express')
const app = express()
const port = 3000

const db = require('./SQLHelper')

var bodyParser = require('body-parser')
require('body-parser-xml')(bodyParser)
app.use(bodyParser.json({ limit:'100mb' }))
app.use(bodyParser.urlencoded({ limit:'100mb', extended: true }))

app.all('*', function (req, res, next) {
  res.header("Access-Control-Allow-Origin", '*')
  res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')
  res.header('Access-Control-Allow-Headers', 'X-Requested-With,Content-Type,token');
  next()
})

app.get('/getTest', (req, res) => {
  res.send({ msg:'Get Hello World!' })
})

app.post('/login',async (req, res) => {
  let userName = req.body.userName,passWord = req.body.passWord
  if(!userName || !passWord) {
    res.send({ errorMsg:'请输入用户名和密码' })
    return
  }
  let user = await db.query(`select * from user where userName="${userName}";`)
  if(!user[0]) {
    res.send({ errorMsg:'用户名不存在' })
    return
  }
  user = user[0]
  if(user.passWord == passWord){
    res.send({ msg:'登录成功' })
  }else{
    res.send({ errorMsg:'密码错误' })
  }  
})

app.get('/user/:id', (req, res) => {
  let params = req.params
  let header = req.headers
  res.send({msg:'aaa'})
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
>>>>>>> 44f7ea2 (newbranch)
})